﻿using ClockInLearning.Db;
using ClockInLearning.Dto;
using ClockInLearning.Impl;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ClockInLearning.Controllers
{
    /// <summary>
    /// 用户信息Controller
    /// </summary>
    [ApiController]
    [Route("userinfo")]
    public class UserInfoController : Controller
    {
        private readonly UserInfoApplication _application;

        public UserInfoController(AppDbContext context)
        {
            _application = new UserInfoApplication(context);
        }

        [HttpGet("test")]
        [ServiceFilter(typeof(ApiFilter))]
        public async Task<UserInfoOutputDto> Get()
        {
            return await _application.Test();
        }

        // 校验token
        // 注意传入参数为单一字符串，必须加上[FromBody]才能从传入上下文的body中获取，且传入时应为字符串且必须额外加上双引号（见前端请求写法）
        [HttpPost("checkToken")]
        public async Task<ServiceResult> CheckToken([FromBody] string token)
        {
            return await _application.CheckToken(token);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public async Task<ServiceResult<UserInfoLoginDto>> Login(LoginDto dto)
        {
            return await _application.Login(dto);
        }

        [HttpPost("logout")]
        [ServiceFilter(typeof(ApiFilter))]
        public async Task<ServiceResult> Logout()
        {
            return await _application.Logout();
        }

        [HttpPost("regist")]
        public async Task<ServiceResult<UserInfoLoginDto>> Regist(RegistDto dto)
        {
            return await _application.Regist(dto);
        }

        [HttpPost("modifyPwd")]
        [ServiceFilter(typeof(ApiFilter))]
        public async Task<ServiceResult> ModifyPwd(ModifyPwdDto dto)
        {
            return await _application.ModifyPwd(dto);
        }

        [HttpPost("getUserInfo")]
        [ServiceFilter(typeof(ApiFilter))]
        public async Task<ServiceResult<UserInfoOutputDto>> GetUserInfo()
        {
            return await _application.GetUserInfo();
        }

        [HttpPost("getUserInfoAndWeekStatic")]
        [ServiceFilter(typeof(ApiFilter))]
        public async Task<ServiceResult<UserInfoAndWeekStaticDto>> GetUserInfoAndWeekStatic()
        {
            return await _application.GetUserInfoAndWeekStatic();
        }
        

        [HttpPost("update")]
        [ServiceFilter(typeof(ApiFilter))]
        public async Task<ServiceResult> Update(UserInfoInputDto dto)
        {
            return await _application.Update(dto);
        }

        [HttpPost("queryRelationList")]
        [ServiceFilter(typeof(ApiFilter))]
        public async Task<ServiceResult<List<UserListDto>>> QueryRelationUsers()
        {
            return await _application.QueryRelationUsers();
        }
    }
}
